package com.goldplusgold.live.channel;

import com.goldplusgold.live.protobuf.ChatProto;
import com.goldplusgold.live.utils.UuidUtil;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * Created by Administrator on 2017/7/14.
 */

public class LiveHeartbeatHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        super.userEventTriggered(ctx, evt);
        if (evt instanceof IdleStateEvent) {
            ChatProto.ChatRequest request = ChatProto.ChatRequest.newBuilder()
                    .setId(UuidUtil.uuidString())
                    .setCmd(ChatProto.Cmd.PING)
                    .build();
            ctx.channel().writeAndFlush(request);
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}
